/*
 * Copyright (C) Michael Martin & Antonio Carrozzini (TBJD)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package org.tbjd.client.factories;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.tbjd.client.beans.Resource;
import org.tbjd.client.beans.Task;
import org.tbjd.client.constants.TimeConstants;

import com.google.gwt.user.client.Random;

public class TaskFactory {

	static private int nextId = 0;
	static private List<Task> tasks = new ArrayList<Task>();

	public static List<Task> getInstances(int n) {
		int position = 0;
		Task task = null;
		for (int i = 0; i < n; i++) {
			int nbChildren = Random.nextInt(4);

			task = new Task();
			task.setId(getNextId());
			task.setBeginDate(new Date(new Date().getTime() + (TimeConstants.DAY_IN_MILLIS * Random.nextInt(5))));
			task.setLevel(position);

			if (nbChildren > 0) {
				task.setName("Phase " + i);
				task.setType(Task.PHASE);

			} else {
				task.setName("Tache " + i);
				task.setType(Task.TASK);
				task.setDuration(1 + Random.nextInt(96));
				task.setProgress(1 + Random.nextInt(100));
			}

			for (int j = 0; j < nbChildren; j++) {
				Task childTask = new Task();
				childTask.setBeginDate(new Date(new Date().getTime() + (TimeConstants.DAY_IN_MILLIS * Random.nextInt(20))));
				childTask.setId(getNextId());
				childTask.setType(Task.TASK);
				childTask.setName("Tache enfant" + i + ":" + j);
				childTask.setProgress(1 + Random.nextInt(100));
				childTask.setLevel(++position);
				childTask.setDuration(1+Random.nextInt(20));
				childTask.setEndDate(new Date((long) (childTask.getBeginDate().getTime() + (childTask.getDuration() / TimeConstants.HOUR_IN_MILLIS))));
				childTask.setParent(task);

			}
			tasks.add(task);
			position++;
		}
		// display();
		return tasks;
	}
	
	public static List<Task> getInstancesWithOnePhaseAndThreeTasks() {
		List<Task> tasks = new ArrayList<Task>();
		
		Task phase = new Task();
		phase.setName("Phase 1");
		phase.setLevel(0);
		phase.setCollapsed(false);
		phase.setId(getNextId());
		
		Task task1 = new Task();
		task1.setBeginDate(new Date(new Date().getTime() + (TimeConstants.DAY_IN_MILLIS * Random.nextInt(20))));
		task1.setId(getNextId());
		task1.setType(Task.TASK);
		task1.setName("Tache enfant 0:1");
		task1.setProgress(1 + Random.nextInt(100));
		task1.setLevel(1);
		task1.setDuration(1+Random.nextInt(20));
		task1.setEndDate(new Date((long) (task1.getBeginDate().getTime() + (task1.getDuration() / TimeConstants.HOUR_IN_MILLIS))));
		task1.setParent(phase);
		
		Task task2 = new Task();
		task2.setBeginDate(new Date(new Date().getTime() + (TimeConstants.DAY_IN_MILLIS * Random.nextInt(20))));
		task2.setId(getNextId());
		task2.setType(Task.TASK);
		task2.setName("Tache enfant 0:2");
		task2.setProgress(1 + Random.nextInt(100));
		task2.setLevel(2);
		task2.setDuration(1+Random.nextInt(20));
		task2.setEndDate(new Date((long) (task2.getBeginDate().getTime() + (task2.getDuration() / TimeConstants.HOUR_IN_MILLIS))));
		task2.setParent(phase);
		
		System.out.println(task1.getId());
		System.out.println(task2.getId());
		
		tasks.add(phase);
		
		return tasks;
	}
	
	public static List<Task> getInstancesWithResources(int n) {
		List<Task> tasks = getInstances(n);
		List<Resource> resources = new ArrayList<Resource>();
		
		for (Task task : tasks) {
			// empty the list of resources
			resources.clear();
			
			// define a random list of resources
			for (int i = 0; i < Random.nextInt(3); i++) {
				
				// add the to the task
				task.addResource(ResourceFactory.getInstance());
				
			}
			
		}
		
		return tasks;
	}
	
	public static List<Task> getInstancesWithAnEmptyPhase() {
		List<Task> tasks = new ArrayList<Task>();
		
		Task task1 = new Task();
		task1.setBeginDate(new Date(new Date().getTime() + (TimeConstants.DAY_IN_MILLIS * Random.nextInt(20))));
		task1.setId(getNextId());
		task1.setType(Task.TASK);
		task1.setName("Tache enfant 0:1");
		task1.setProgress(1 + Random.nextInt(100));
		task1.setLevel(0);
		task1.setDuration(1+Random.nextInt(20));
		task1.setEndDate(new Date((long) (task1.getBeginDate().getTime() + (task1.getDuration() / TimeConstants.HOUR_IN_MILLIS))));
		
		Task phase = new Task();
		phase.setName("Phase 1");
		phase.setLevel(1);
		phase.setDuration(24);
		phase.setCollapsed(false);
		phase.setId(getNextId());
		phase.setType(Task.PHASE);
		phase.setBeginDate(new Date());
		phase.setEndDate(new Date((long) (phase.getBeginDate().getTime() + (phase.getDuration() * TimeConstants.HOUR_IN_MILLIS))));
		
		tasks.add(task1);
		tasks.add(phase);
		
		return tasks;
	}

	static void display() {
		for (Task eachTask : tasks) {
			System.out.println("id:" + eachTask.getId() + " - level:" + eachTask.getLevel() + " - type:" + eachTask.getType() + " - child:"
					+ eachTask.getLstChildren().size());
			for (Task eachChild : eachTask.getLstChildren()) {
				System.out.println("---------> id:" + eachChild.getId() + " - level:" + eachChild.getLevel() + " - duration:"
						+ eachChild.getDuration() + " - type:" + eachChild.getType() + " - child:" + eachChild.getLstChildren().size());
			}
		}
	}

	private static int getNextId() {
		return --nextId;
	}
	
	/**
	 * Return an empty Task
	 * 
	 * @return an empty Task
	 */
	public static Task getAnEmptyTask() {

		Task task = new Task();

		task.setId(TaskFactory.getNextId());
		task.setBeginDate(new Date());
		task.setEndDate(new Date(task.getBeginDate().getTime() + TimeConstants.DAY_IN_MILLIS * 2));
		task.setLevel(-1);
		task.setDuration(48f);
		task.setEffort(48f);
		task.setName("untitled task [" + task.getId() + "]");

		return task;
	}
}
